package com.piece.flowable.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.module.flowable.dto.WfModel;
import com.piece.module.flowable.service.WfModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@Api(tags = {"流程模型管理"})
@RequestMapping("/api/workflow/model")
public class WfModelController extends FrameWorkController {

    @Resource
    private WfModelService wfModelService;

    @PostMapping("/table")
    @AclPermission(code = "flowable:model:view")
    @ApiOperation(value = "查询流程模型分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = wfModelService.findPage(queryTable, null);
        return success(dataTable);
    }

    @PostMapping("/historyTable")
    @AclPermission(code = "flowable:model:view")
    @ApiOperation(value = "查询流程模型版本分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse historyTable() {
        QueryTable queryTable = initTable();
        String modelKey = getRequest().getParameter("modelKey");
        DataTable dataTable = wfModelService.findPage(queryTable, modelKey);
        return success(dataTable);
    }

    @PostMapping("/insert")
    @AclPermission(code = "flowable:model:view")
    @ApiOperation(value = "加载流程模型表单数据")
    @ApiImplicitParam(name="modelId", value = "主键")
    public AjaxResponse insert() {
        String modelId = Convert.toStr(getRequest().getParameter("modelId"), null);
        if (StringUtil.isNotEmpty(modelId)) {
            return success(wfModelService.findModel(modelId));
        }

        WfModel wfModel = new WfModel();
        initDB(wfModel);
        wfModelService.insert(wfModel);
        return success(wfModel);
    }

    @SubmitPermission
    @PostMapping("/update")
    @AclPermission(code = "flowable:model:edit")
    @ApiOperation(value = "修改流程模型")
    @ApiImplicitParam(value = "流程模型数据", dataTypeClass = WfModel.class, type = "body")
    public AjaxResponse update() {
        WfModel wfModel = new WfModel();
        initDB(wfModel);
        wfModelService.update(wfModel);
        return success();
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "flowable:model:edit")
    @ApiOperation(value = "保存流程模型")
    @ApiImplicitParam(value = "流程模型数据", dataTypeClass = WfModel.class, type = "body")
    public AjaxResponse save() {
        WfModel wfModel = new WfModel();
        initDB(wfModel);
        wfModelService.saveModel(wfModel);
        return success();
    }

    @SubmitPermission
    @PostMapping("/latest/{modelId}")
    @AclPermission(code = "flowable:model:edit")
    @ApiOperation(value = "设为最新流程模型")
    @ApiImplicitParam(name="modelId", value = "主键", type = "path")
    public AjaxResponse latest(@PathVariable String modelId) {
        wfModelService.latestModel(modelId);
        return success();
    }

    @SubmitPermission
    @PostMapping("/deploy/{modelId}")
    @AclPermission(code = "flowable:model:deploy")
    @ApiOperation(value = "部署流程模型")
    @ApiImplicitParam(name="modelId", value = "主键", type = "path")
    public AjaxResponse deploy(@PathVariable String modelId) {
        try {
            wfModelService.deployModel(modelId);
            return success();
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.MESSAGE_ERROR), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "flowable:model:delete")
    @ApiOperation(value = "删除流程模型")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            wfModelService.delete(Convert.toStrList(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @GetMapping("/readXml/{modelId}")
    @AclPermission(code = "flowable:model:view")
    @ApiOperation(value = "加载流程模型详细信息")
    @ApiImplicitParam(name="modelId", value = "主键", type = "path")
    public AjaxResponse readXml(@PathVariable String modelId) {
        return success(wfModelService.readBpmnXml(modelId));
    }
}
